$(function() {
	//panel初始化
	$('#dMenuPanel').panel({
		border:false
	});

	
	//菜单按钮初始化
	$('#addMenuBtn').linkbutton({plain:true,iconCls:'icon-fun-add'});
	$('#editMenuBtn').linkbutton({plain:true,iconCls:'icon-fun-edit'});
	$('#delMenuBtn').linkbutton({plain:true,iconCls:'icon-fun-del'});
	$('#closeMenuBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	//添加窗口
	$('#dMenuWindow').window({
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-app',
		width:310,
		height:390
	});

	//treegrid初始化
	$('#dMenuTreeGrid').treegrid({
		nowrap : false,
		collapsible:true,
		loadMsg:'数据加载中......',
		url:'menuAjax!loadMenuTreeGrid',
		idField : 'id',
		treeField : 'name',
		border:false,
		columns : [[
		    	{field:'name',title:'名称',width:250},
		    	{field:'menuOrder', title:'位置',width:50},
		        {field:'target',title:'目标',width:250},
		        {field:'code',title:'代码',width:100},
		        {field:'iconCls', title:'图标',width:100},
		        {field:'des', title:'描述',width:230}
		]],
		onDblClickRow: function(row){
			$(this).treegrid('toggle',row.id);
		}		
	});
	
	//主菜单下拉菜单
	$('#dMainMenus').combobox({
		width:201,
		url:'menuAjax!loadMainMenusData',
		valueField:'id',
		textField:'name',
		multiple:false,
		onSelect:function(record){
			$('#dParentMenus').combotree({ 
				width:201,
				url:'menuAjax!loadSubMenuTree?mainId=' + record.id
				}); 
			$('#dParentMenus').combotree('setValue', 0);
		}
	});
	
	//父菜单树菜单
	$('#dParentMenus').combotree({ 
		width:201,
		url:'menuAjax!loadSubMenuTree?mainId=0'
		}); 

});

//打开窗口
function openMenuWindow(type) {
	clearTable();
	if(type == 'del'){
		delMenu();
		return;
	}else if(type == 'add'){
		$('#menuMethodName').val('add');
		$('#dMenuWindow').window({title:'增加'});
		$('#submitMenuButton').val('增加');
		$('#dMainMenus').combobox('select', 0);
		$('#dParentMenus').combotree('setValue', 0);
		$('#menuOrder').val(0);
	}else if(type == 'edit'){
		var selectedRow = $('#dMenuTreeGrid').treegrid('getSelected');
		if(selectedRow==null){
			$.messager.alert('提示','请先选择一条记录!');
			return;
		}else{
			$('#menuMethodName').val('editAll');
			$('#dMenuWindow').window({title:'编辑'});
			$('#submitMenuButton').val('编辑');
			$('#dMainMenus').combobox('setValue',selectedRow.mainId);
			$('#dParentMenus').combotree('setValue',selectedRow.parentId);
			$('#menuName').val(selectedRow.name);
			$('#menuCode').val(selectedRow.code);
			$('#menuTarget').val(selectedRow.target);
			$('#menuOrder').val(selectedRow.menuOrder);
			$('#menuIcon').val(selectedRow.iconCls);
			$('#menuDes').val(selectedRow.des);
		}
	}else{
		$.messager.alert('提示','非法操作!',"error");
		return;
	}
	$('#dMenuWindow').window('open');	
	$('#menuName').focus();
}


function delMenu(){
	var selectedNode = $('#dMenuTreeGrid').treegrid('getSelected');
	//alert(selectedNode.subMenu);
	if(selectedNode==null){
		$.messager.alert('提示','请先选择一个菜单!');
		return;
	}else if(selectedNode.subMenu == false){
		$.messager.alert('提示','请先删除所有子菜单!');
		return;
	}else{
		$.messager.confirm('提示', '确认要删除此菜单吗?', function(r){
			if (r){
				$.post('menuAjax!delete', {id:selectedNode.id},
			        	function(data){
							$('#dMenuTreeGrid').treegrid('reload');
			        	}
			        	, 'json');
			}

		});
	}
}

//提交操作(添加模块,修改模块)
function submitMenu(){
	var menuMethodName = $('#menuMethodName').val();
	var menuName = $('#menuName').val();
	var menuTarget = $('#menuTarget').val();
	var menuOrder = $('#menuOrder').val();
	var menuIcon = $('#menuIcon').val();
	var menuCode = $('#menuCode').val();
	var menuDes = $('#menuDes').val();
	var parentId = $('#dParentMenus').combotree('getValue');
	var mainId = $('#dMainMenus').combobox('getValue');

	if(menuName == ''){
		$.messager.alert('提示','名称不能为空!');
		return;
	}
	
	switch(menuMethodName){
		//添加菜单
		case 'add':
			//var node = $('#dMenuTreeGrid').treegrid('getSelected');
			$.post('menuAjax!add', {name:menuName, target:menuTarget, menuOrder:menuOrder, code:menuCode, iconCls:menuIcon, des:menuDes, parentId:parentId, mainId: mainId},		    		
		        	function(data){
						$("#dMenuTreeGrid").treegrid('reload');
						$('#dMainMenus').combobox('reload');
						$('#dParentMenus').combobox('reload');
		        	}
		        	, 'json');
			break;
		//编辑菜单
		case 'editAll':
			var row = $('#dMenuTreeGrid').treegrid('getSelected');	
		    $.post('menuAjax!edit', {id:row.id, name:menuName, target:menuTarget, menuOrder:menuOrder, code:menuCode, iconCls:menuIcon, des:menuDes, parentId:parentId, mainId: mainId},		    		
		        	function(data){
		    			$("#dMenuTreeGrid").treegrid('reload');
		        	}
		        	, 'json');
			break;
	}
	
	$('#dMenuWindow').window('close');	
}

function clearTable(){
	$('#menuName').val('');
	$('#menuTarget').val('');
	$('#menuOrder').val('');
	$('#menuCode').val('');
	$('#menuIcon').val('');
	$('#menuDes').val('');
	$('#dParentMenus').combotree('clear');
	$('#dMainMenus').combobox('clear');
}

